package org.despair2D.ui.supportClasses 
{
	import org.despair2D.Observer;
	import org.despair2D.ui.events.InteractiveEvent;
	import org.despair2D.ui.events.ManipulateEvent;
	import org.despair2D.ui.Fusion;
	
	import org.despair2D.core.ns_despair;
	use namespace ns_despair;
	
	
public class AbstractButton extends Fusion 
{

	public function AbstractButton()
	{
		this._addListeners()
	}
	
	
	
	/** 是否启用按钮效果 **/
	final public function get effectEnabled() : Boolean
	{
		return m_effectEnabled
	}
	
	final public function set effectEnabled( b:Boolean ) : void
	{
		if (m_effectEnabled == b)
		{
			return
		}
		
		m_effectEnabled = b
		
		if (b)
		{
			if (this.pressed)
			{
				if (m_pressListener)
				{
					m_pressListener.execute();
				}
			}
			
			else if (this.hovered)
			{
				if (m_overListener)
				{
					m_overListener.execute();
				}
				
				else if (m_leaveListener)
				{
					m_leaveListener.execute();
				}
			}
			
			else if (m_leaveListener)
			{
				m_leaveListener.execute(); 
			}
		}
	}
	
	
	/** 滑出效果 **/
	final public function addLeaveEffectListener( listener:Function, priority:int = 0 ) : void
	{
		(m_leaveListener ||= new Observer).addListener(listener, priority);
	}
	
	
	/** 滑入效果 **/
	final public function addHoverEffectListener( listener:Function, priority:int = 0 ) : void
	{
		(m_overListener ||= new Observer).addListener(listener, priority);
	}
	
	
	/** 按下效果 **/
	final public function addPressEffectListener( listener:Function, priority:int = 0 ) : void
	{
		(m_pressListener ||= new Observer).addListener(listener, priority);
	}
	
	
	/** 失效效果 **/
	final public function addDisableEffectListener( listener:Function, priority:int = 0 ) : void
	{
		(m_disableListener ||= new Observer).addListener(listener, priority);
	}
	
	
	
	ns_despair function _addListeners() : void
	{
		this.addEventListener(ManipulateEvent.OVER,                 overEffect);
		this.addEventListener(ManipulateEvent.LEAVE,                leaveEffect);
		this.addEventListener(ManipulateEvent.RELEASE,              releaseEffect);
		this.addEventListener(ManipulateEvent.PRESS,                pressEffect);
		this.addEventListener(InteractiveEvent.INTERACTIVE_CHANGE,  disableEffect);
	}
	

	override ns_despair function recycle() : void
	{
		if (m_leaveListener)
		{
			m_leaveListener.dispose();
			m_leaveListener = null;
		}
		if(m_overListener)
		{
			m_overListener.dispose();
			m_overListener = null;
		}
		if (m_pressListener)
		{
			m_pressListener.dispose();
			m_pressListener = null;
		}
		if (m_disableListener)
		{
			m_disableListener.dispose()
			m_disableListener = null
		}
		
		super.recycle();
	}
	

	ns_despair static function overEffect( e:ManipulateEvent ) : void
	{
		var btn:AbstractButton = e.target as AbstractButton;
		
		if (!btn.effectEnabled)
		{
			return
		}
		
		if (btn.pressed)
		{
			if (btn.m_pressListener)
			{
				btn.m_pressListener.execute();
			}
		}
		
		else
		{
			if (btn.m_overListener)
			{
				btn.m_overListener.execute();
			}
		}
	}
	
	
	ns_despair static function leaveEffect( e:ManipulateEvent ) : void
	{
		var btn:AbstractButton = e.target as AbstractButton;
		
		if (!btn.effectEnabled)
		{
			return
		}
		
		if (btn.pressed)
		{
			if (btn.m_overListener)
			{
				btn.m_overListener.execute();
			}
		}
		
		else if (btn.m_leaveListener)
		{
			btn.m_leaveListener.execute();
		}
	}
	
	
	ns_despair static function pressEffect( e:ManipulateEvent ) : void
	{
		var btn:AbstractButton = e.target as AbstractButton;
		
		if (!btn.effectEnabled)
		{
			return
		}
		
		if (btn.m_pressListener)
		{
			btn.m_pressListener.execute();
		}
	}
	
	
	ns_despair static function releaseEffect( e:ManipulateEvent ) : void
	{
		var btn:AbstractButton = e.target as AbstractButton;
		
		if (!btn.effectEnabled)
		{
			return
		}
		
		if (btn.hovered)
		{
			if (btn.m_overListener)
			{
				btn.m_overListener.execute();
			}
			
			else if (btn.m_leaveListener)
			{
				btn.m_leaveListener.execute();
			}
		}
		
		else if (btn.m_leaveListener)
		{
			btn.m_leaveListener.execute(); 
		}
	}
	
	
	ns_despair static function disableEffect( e:InteractiveEvent ) : void
	{
		var btn:AbstractButton = e.target as AbstractButton;
		
		if (e.enabled)
		{
			if (btn.hovered)
			{
				if (btn.m_overListener)
				{
					btn.m_overListener.execute();
				}
				
				else if (btn.m_leaveListener)
				{
					btn.m_leaveListener.execute();
				}
			}
			
			else if (btn.m_leaveListener)
			{
				btn.m_leaveListener.execute();
			}
		}
		
		else if (btn.m_disableListener)
		{
			btn.m_disableListener.execute()
		}
	}
	
	
	
	ns_despair var m_leaveListener:Observer;
	
	ns_despair var m_overListener:Observer;
	
	ns_despair var m_pressListener:Observer;
	
	ns_despair var m_disableListener:Observer;
	
	ns_despair var m_effectEnabled:Boolean = true
	
}

}